package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDataBean;
import com.netease.nim.uikit.common.util.log.LogUtil;

import java.text.MessageFormat;

/**
 * Created by Administrator on 2018/4/7.
 */

public class OrderManageAddressAdapter extends BaseRecyclerViewAdapter<OrderUserAddressDataBean.DataBean.AddressBean> {

    private Context mContext;
    private OnItemButtonClickListener mOnItemButtonClickListener;

    //控件是否被点击,默认为false，如果被点击，改变值，key传递position值
    private int key = 0;
    private boolean onclick = false;
    private static final String TAG = "OrderManageAddressAdapt";
    /**
     * 判断是否是第一次进入，如果是就根据数据中的字段来修改按钮，如果不是按照用户点击事件修改按钮
     */
    private Boolean isFirst=true;

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTvAddressName;
        TextView mTvAddressPhone;
        TextView mTvAddress;
        TextView mTvIsDefaultAddress;
        TextView mTvUserAddressTag;
        ImageView mIvChoose;
        RelativeLayout mRlAddressChoose;
        TextView mTvBtnAddressEdit;
        TextView mTvBtnAddressDelete;

        public ViewHolder(View view) {
            super(view);
            //姓名
            mTvAddressName = view.findViewById(R.id.tv_user_address_name);
            //电话
            mTvAddressPhone = view.findViewById(R.id.tv_user_address_phone);
            //地址
            mTvAddress = view.findViewById(R.id.tv_user_address);
            //默认标签
            mTvIsDefaultAddress = view.findViewById(R.id.tv_is_default_address);
            //自定义标签
            mTvUserAddressTag = view.findViewById(R.id.tv_user_address_tag);
            //默认地址按钮
            mIvChoose = view.findViewById(R.id.iv_address_choose);
            //编辑按钮
            mTvBtnAddressEdit = view.findViewById(R.id.tv_address_edit);
            //默认地址布局
            mRlAddressChoose = view.findViewById(R.id.rl_order_address_choose);
            //删除按钮
            mTvBtnAddressDelete = view.findViewById(R.id.tv_address_delete);
        }

        public void setData(OrderUserAddressDataBean.DataBean.AddressBean bean) {
            //显示用户名，用户手机，用户地址，如果def等于1为默认地址，用户标签如果不为空就显示标签
            mTvAddressName.setText(bean.getTruename());
            mTvAddressPhone.setText(bean.getTelephone());
            //省
            String province = bean.getProvince();
            //市
            String city = bean.getCity();
            //区
            String county = bean.getCounty();
            mTvAddress.setText(MessageFormat.format("{0} {1} {2} {3}", province, city, county, bean.getAddress()));

            // 设置默认地址按钮状态
            if (isFirst) {
                LogUtil.i(TAG,"根据数据设置默认");
                mIvChoose.setImageDrawable(TextUtils.equals(bean.getDef(), "1") ?
                        ContextCompat.getDrawable(mContext, R.drawable.ic_radio_on_blue) :
                        ContextCompat.getDrawable(mContext, R.drawable.ic_radio_off_gray)
                );
                // 设置是否显示默认标签
                mTvIsDefaultAddress.setVisibility(TextUtils.equals(bean.getDef(), "1") ?
                        View.VISIBLE : View.GONE);
            } else {
                LogUtil.i(TAG,"根据点击事件设置默认");
                mIvChoose.setImageDrawable(bean.isSelected() ?
                        ContextCompat.getDrawable(mContext, R.drawable.ic_radio_on_blue) :
                        ContextCompat.getDrawable(mContext, R.drawable.ic_radio_off_gray)
                );
                mTvIsDefaultAddress.setVisibility(bean.isSelected() ?
                        View.VISIBLE : View.GONE
                );
            }
            // 设置用户标签类型
            mTvUserAddressTag.setVisibility(TextUtils.isEmpty(bean.getLabel()) ? View.GONE : View.VISIBLE);
            mTvUserAddressTag.setText(TextUtils.isEmpty(bean.getLabel()) ? "" : bean.getLabel());
        }

        public void setListener(final OrderUserAddressDataBean.DataBean.AddressBean bean) {

            //编辑按钮
            mTvBtnAddressEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemButtonClickListener != null) {
                        //接口实例化后的而对象，调用重写后的方法
                        mOnItemButtonClickListener.onEditClick(bean, getAdapterPosition());
                        isFirst=true;
                    }
                }
            });

            //删除按钮
            mTvBtnAddressDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemButtonClickListener != null) {
                        mOnItemButtonClickListener.onDeleteClick(bean, getAdapterPosition());
                    }
                }
            });

            //选择默认按钮
            mRlAddressChoose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isFirst=false;
                    if (mOnItemButtonClickListener != null) {
                        mOnItemButtonClickListener.onclick(bean, getAdapterPosition());
                    }
           //         key = getAdapterPosition();
              //              onclick = true;
                }
            });
//            //如果点击了当前的item就改变当前item默认，其他的也改变
//            if (key == getAdapterPosition() && onclick) {
//                mIvChoose.setImageResource(R.drawable.ic_radio_on_blue);
//                mTvIsDefaultAddress.setVisibility(View.VISIBLE);
//            } else if (onclick) {
//                mIvChoose.setImageResource(R.drawable.ic_radio_off_gray);
//                mTvIsDefaultAddress.setVisibility(View.GONE);
//            }

        }
    }

    public OrderManageAddressAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_order_address_manage, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderUserAddressDataBean.DataBean.AddressBean bean = mList.get(position);
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).setData(bean);
            ((ViewHolder) holder).setListener(bean);
        }
    }

    /**
     * 按钮点击事件需要的方法
     */
    public void buttonSetOnclick(OnItemButtonClickListener buttonInterface) {
        this.mOnItemButtonClickListener = buttonInterface;
    }

    /**
     * 按钮点击事件对应的接口
     */
    public interface OnItemButtonClickListener {
        void onclick(OrderUserAddressDataBean.DataBean.AddressBean bean, int position);

        void onDeleteClick(OrderUserAddressDataBean.DataBean.AddressBean bean, int position);

        void onEditClick(OrderUserAddressDataBean.DataBean.AddressBean bean, int position);

    }
}
